package org.sunrise.game.rpc.message;

import org.sunrise.game.common.log.LogCore;
import org.sunrise.game.common.message.MessageUtils;
import org.sunrise.game.common.network.message.ServerMessageManager;
import org.sunrise.game.rpc.function.Call;
import org.sunrise.game.rpc.function.CallUtils;

/**
 * pulseSender 将rpc处理后的返回值发给其他节点的client
 * pulseHandlerOne 处理其他节点的client发来的call
 */
public class RpcServerMessageManager extends ServerMessageManager {
    public RpcServerMessageManager(String nodeId) {
        super(nodeId);
    }

    @Override
    protected void pulseHandlerOne(Object data) {
        Call message = null;
        if (data instanceof byte[]) {
            message = MessageUtils.fromMessage((byte[]) data, Call.class);
        } else if (data instanceof Call) {
            message = (Call) data;
        }
        if (message == null) {
            return;
        }
        LogCore.RpcServer.debug("recv call, cur NodeId = { {} }, from NodeId = { {} }, rpcId = {}, messageId = { {} }, data = { {} }", getNodeId(), message.getNodeId(), message.getRpcId(), message.getMessageId(), message.getData());
        CallUtils.handler(message);
    }
}
